package com.ruoyi.common.utils;

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.photo.Photo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ImageResizeEnhance {
    static {
        System.load("D:\\Program Files\\opencv\\build\\java\\x64\\opencv_java490.dll");
    }


    public static void imageResize(String targetPath) throws IOException {

        // 指定要遍历的根目录路径
        Path rootPath = Paths.get(targetPath);
        Files.walk(rootPath)
                .filter(Files::isRegularFile) // 过滤出普通文件
                .forEach(path -> {
                    String inputImagePath = path.toString();
                    Mat image = Imgcodecs.imread(inputImagePath);

                    if (image.empty()) {
                        System.out.println("无法加载图片: " + inputImagePath);
                        return;
                    }

                    // 获取图像的宽高
                    int width = image.cols();
                    int height = image.rows();

                    // 判断是否需要扩展
                    if (width < 1080 || height < 1080) {
                        // 计算新的尺寸，保持宽高比
                        double scale = Math.max(1080.0 / width, 1080.0 / height);
                        int newWidth = (int) (width * scale);
                        int newHeight = (int) (height * scale);

                        // 扩展图像尺寸，使用高质量的插值方法
                        Mat resizedImage = new Mat();
                        // 示例: 提高对比度 (alpha > 1.0) 并增加亮度 (beta > 0)
                        Mat enhancedImage = adjustContrastAndBrightness(resizedImage, 1.2, 20);
                        Imgproc.resize(image, enhancedImage, new Size(newWidth, newHeight), 0, 0, Imgproc.INTER_LANCZOS4);
                         // 应用锐化滤镜，进一步提升清晰度
                        Mat sharpenedImage = sharpenImage(enhancedImage);
                        //降噪
                        Photo.fastNlMeansDenoisingColored(enhancedImage, enhancedImage, 10, 10, 7, 21);

                        // 保存处理后的图像
                        Imgcodecs.imwrite(path.toString(), sharpenedImage);
                        System.out.println(path.toString()+"图片扩展和清晰度提升完成！");
                    } else {
                        System.out.println("图片已经符合要求，无需扩展。");
                    }
                });
    }

    public static void main(String[] args) throws IOException {

        // 读取输入图像
        String inputImagePath = "E:/workdata/码诚科技/物流采集/物流样例/物流";
        imageResize(inputImagePath);
    }
    private static Mat adjustContrastAndBrightness(Mat image, double alpha, int beta) {
        Mat newImage = new Mat();
        image.convertTo(newImage, -1, alpha, beta); // alpha控制对比度，beta控制亮度
        return newImage;
    }



    // 锐化滤镜函数，使用卷积核来增强图像的锐度
    private static Mat sharpenImage(Mat image) {
        Mat sharpenedImage = new Mat();

        // 锐化卷积核
        Mat kernel = new Mat(3, 3, CvType.CV_32F) {
            {
                put(0, 0, 0, -1, 0);
                put(1, 0, -1, 5, -1);
                put(2, 0, 0, -1, 0);
            }
        };

        // 使用卷积操作应用锐化
        Imgproc.filter2D(image, sharpenedImage, -1, kernel);
        return sharpenedImage;
    }

}


